package com.qiaoyanqing.textdemoqyq7.Fragments;


import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.qiaoyanqing.textdemoqyq7.Activities.WebViewActivity;
import com.qiaoyanqing.textdemoqyq7.Adapters.BaikeAdapter;
import com.qiaoyanqing.textdemoqyq7.AsyncTasks.BaikeAsyncTask;
import com.qiaoyanqing.textdemoqyq7.Beans.Baike;
import com.qiaoyanqing.textdemoqyq7.CallBacks.BaikeCB;
import com.qiaoyanqing.textdemoqyq7.R;
import com.qiaoyanqing.textdemoqyq7.SQL.MySQLiteopenHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class ShujuFragment extends Fragment implements AbsListView.OnScrollListener, AdapterView.OnItemClickListener {
    private ListView mListView;
    private List<Baike> mData=new ArrayList<>();
    // 数据库
    private MySQLiteopenHelper mHelper;

    private static final String BASE_URL = "http://sns.maimaicha.com/api?apikey=b4f4ee31a8b9acc866ef2afb754c33e6&format=json&method=";
    public static final int TYPE_INFOMATION = 52;// 资讯
    public static final int TYPE_DATA = 54;// 数据
    private static final String PAGE = "&page=";
    // 频道接口
    public static final String CHANNEL_URL = BASE_URL + "news.getListByType&row=15&type=";
    private BaikeAdapter mBaikeAdapter;


    public ShujuFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_shuju, container, false);
        mListView= (ListView) view.findViewById(R.id.shuju_listview);
        mHelper=new MySQLiteopenHelper(getContext());
        initData();
        initListview();
        initPagechangge();
        //设置条目点击事件
        initItemclick();

        return view;
    }

    private void initItemclick() {

        mListView.setOnItemClickListener(this);

    }

    private void initPagechangge() {
        mListView.setOnScrollListener(this);
    }

    private void initListview() {
        mBaikeAdapter = new BaikeAdapter(getContext(),mData);

        mListView.setAdapter(mBaikeAdapter);

    }

    private void initData() {
        new BaikeAsyncTask(new BaikeCB() {
            @Override
            public void senddata(List<Baike> data) {


                mData.addAll(data);
                mBaikeAdapter.notifyDataSetChanged();

            }
        }).execute(CHANNEL_URL+TYPE_DATA+PAGE+page);

    }

    private boolean istrue = false;
    private int page = 0;
    @Override
    public void onScrollStateChanged(AbsListView absListView, int i) {
        if (istrue && i == SCROLL_STATE_IDLE) {
            page++;
            initData();
        }
    }

    @Override
    public void onScroll(AbsListView absListView, int i, int i1, int i2) {
        if (i + i1 == i2) {
            istrue = true;
        } else {
            istrue = false;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

        SQLiteDatabase db=mHelper.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put("id",mData.get(i).getId());
        values.put("title",mData.get(i).getTitle());
        values.put("source",mData.get(i).getSource());
        values.put("create_time",mData.get(i).getCreate_time());
        db.insert("tea",null,values);
        Toast.makeText(getContext(), "加入到了浏览记录", Toast.LENGTH_SHORT).show();
     //跳转
        Intent intent=new Intent(getContext(), WebViewActivity.class);
        intent.putExtra("page",mData.get(0).getId());
        startActivity(intent);

    }
}
